<?php
header("Content-type:text/html; charset=utf-8");
require_once ('config.php');
require_once (WEBURL.'/include/conn.php');
require_once ('login_status_check.php');
if(!checkLogin()){
	require_once ('admin_login.php');
}
class Channel{
	private $all_channel;
	private $child_channel_information;
	/*标识符,判断调用的是文章操作阶段还是栏目操作阶段,区分的原因是发表文章选择栏目时只能选最底层目录*/
	private $article_or_channel="";
	function __construct(){
		$this->all_channel="";
		$this->child_channel_information="";
	}
	function getAllChannel($article_or_channel){
		$this->all_channel="";
		$this->article_or_channel=$article_or_channel;
		$this->getAllChanelTree();
		return $this->all_channel;
	}
	function getMaxChannelGrade(){
		$sql_get_max_channel_grade="select max(channel_grade) from channel;";
		//echo $sql_get_max_channel_grade;
		$result_get_max_channel_grade=mysql_query($sql_get_max_channel_grade);
		$row_get_max_channel_grade=mysql_fetch_array($result_get_max_channel_grade);
		$this->max_channel_grade=$row_get_max_channel_grade[0];
		return $this->max_channel_grade;
	}
	/*递归获取全部目录结构*/
	function getAllChanelTree($parent_id = 0) {
		/*执行sql查询，获取记录的标题和id*/
		$sql_all_channel_tree = "select channel_name,channel_id,is_bottom,channel_grade from channel where pre_id = $parent_id order by pre_id asc";
		//echo $sql_all_channel_tree;
		$result_all_channel_tree = mysql_query($sql_all_channel_tree);
		while($row_all_channel_tree = mysql_fetch_row($result_all_channel_tree)) {
			$tmp_level=$this->levelShow($row_all_channel_tree[3]);
			if(($this->article_or_channel=="article")&&($row_all_channel_tree[2]==0)){
				$this->all_channel.="<option disabled=disabled value=".$row_all_channel_tree[1].">".$tmp_level.$row_all_channel_tree[0]."</option>";
			}
			else {
				$this->all_channel.="<option value=".$row_all_channel_tree[1].">".$tmp_level.$row_all_channel_tree[0]."</option>";
			}
			/* 递归调用 */
			$this->getAllChanelTree($row_all_channel_tree[1]);
		}
	}

	function levelShow($level){
		switch($level){
			case 1:
				$tmp_level="&nbsp-";
				break;
			case 2:
				$tmp_level="&nbsp&nbsp--";
				break;
			case 3:
				$tmp_level="&nbsp&nbsp&nbsp&nbsp---";
				break;
			case 4:
				$tmp_level="&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp----";
				break;
			default:
				$tmp_level="";
				break;
		}
		return $tmp_level;
	}

	function getChannelNameGrade($channel_id){
		$sql_get_channel_name_grade="select channel_name,channel_grade,is_bottom from channel where channel_id =$channel_id";
		$result_get_channel_name_grade=mysql_query($sql_get_channel_name_grade);
		$row_get_channel_name_grade=mysql_fetch_row($result_get_channel_name_grade);
		return array("0"=>$row_get_channel_name_grade[0],"1"=>$row_get_channel_name_grade[1],"2"=>$row_get_channel_name_grade[2]);
	}
	function getChildChannelInformation(){
		$this->getChildChannelInformationTree();
		return $this->child_channel_information;
	}
	function getChildChannelInformationTree($current_id=0){
		$this->child_channel_information.="<ul>";
		$sql_get_child_channel="select channel_id,channel_name,channel_visit_path,channel_grade,is_bottom from channel where pre_id =$current_id;";
		$result_get_child_channel=mysql_query($sql_get_child_channel) or die(mysql_error());
		while($row_get_child_channel=mysql_fetch_row($result_get_child_channel)){
			$tmp_level=str_replace("-", "&nbsp;&nbsp;",$this->levelShow($row_get_child_channel[3]));
			$article_num=$this->getArticleNumber($row_get_child_channel[0]);
				$link="../channel.php?channel_id=$row_get_child_channel[0]";
			if($row_get_child_channel[4]==0){
				$this->child_channel_information.="<div id='channel_$row_get_child_channel[0]'><li class='parent' onmousemove=\"javascript:this.style.background='#ddd';\" onmouseout=\"javascript:this.style.background='';\">$tmp_level
				<a href='javascript:' title='子栏目' onclick='showChild($row_get_child_channel[0]);'><img class='arror_right' src='images/channel/right.png'/></a>
				<span class='channel_name'>$row_get_child_channel[1][ID:$row_get_child_channel[0]]</span></a><span class='article_visit_path'></span>
				<a href=$link target='_blank'>[预览]</a><span class='tmp'>tmp</span><a href='channel_modify.php?channel_id=$row_get_child_channel[0]'>[修改]</a><span class='tmp'>tmp</span>
				<a href='channel_add.php?pre_id=$row_get_child_channel[0]'>[添加子栏目]</a></li>
				<li class='division'></li></div>";
			}else{
				$this->child_channel_information.="<div id='channel_$row_get_child_channel[0]'><li class='child' onmousemove=\"javascript:this.style.background='#ddd';\" onmouseout=\"javascript:this.style.background='';\">$tmp_level
				<a href='javascript:' title='子栏目' onclick='showChild($row_get_child_channel[0]);'><img class='arror_right' src='images/channel/right.png'/></a>
				<a href='article_show.php?channel_id=$row_get_child_channel[0]' title='查看文章列表'>
				<span class='channel_name'>$row_get_child_channel[1][ID:$row_get_child_channel[0]]</span></a><span class='article_number'>(文档：$article_num)</span><span class='article_visit_path'></span>
				<a href=$link target='_blank'>[预览]</a><span class='tmp'>tmp</span><a href='channel_modify.php?channel_id=$row_get_child_channel[0]'>[修改]</a><span class='tmp'>tmp</span>
				<a href='channel_remove.php?channel_id=$row_get_child_channel[0]&pre_id=$current_id&channel_name=$row_get_child_channel[1]'>[删除]</a><span class='tmp'>tmp</span>
				<a href='channel_add.php?pre_id=$row_get_child_channel[0]'>[添加子栏目]</a></li>
				<li class='division'></li></div>";
			}
			$this->child_channel_information.="</ul>";
			$this->getChildChannelInformationTree($row_get_child_channel[0]);
		}
	}

	function getArticleNumber($channel_id){
		$sql_get_article_number="select count(*) from article_normal where article_main_channel=$channel_id";
		$result_get_article_number=mysql_query($sql_get_article_number) or die(mysql_error());
		$row_get_article_number=mysql_fetch_row($result_get_article_number);
		return $row_get_article_number[0];
	}
	function getFirstChannelId(){
		$result=array();
		$i=0;
		$sql_get_first_channel_id="select channel_id from channel where pre_id=0;";
		$result_get_first_channel_id=mysql_query($sql_get_first_channel_id);
		while($row_get_first_channel_id=mysql_fetch_row($result_get_first_channel_id)){
			$result[$i++]=$row_get_first_channel_id[0];
		}
		return join("|",$result);
	}

	function getOtherChannelId(){
		$result=array();
		$i=0;
		$sql_get_other_channel_id="select channel_id from channel where pre_id>0;";
		$result_get_other_channel_id=mysql_query($sql_get_other_channel_id);
		while($row_get_other_channel_id=mysql_fetch_row($result_get_other_channel_id)){
			$result[$i++]=$row_get_other_channel_id[0];
		}
		return join("|",$result);
	}
	/*获取给定栏目id的栏目信息，供修改栏目信息时使用*/
	function getChannelInformation($channel_id){
		$sql_get_channel_information="select pre_id,channel_name,channel_visit_path,channel_describe,channel_model,article_model,channel_grade from channel where channel_id=$channel_id";
		$result_get_channel_information=mysql_query($sql_get_channel_information);
		$row_get_channel_information=mysql_fetch_row($result_get_channel_information);
		if(!empty($row_get_channel_information)){
			return join("|",$row_get_channel_information);
		}
		return "";
	}
	/*获取给定文章id的文章信息，供修改文章时使用*/
	function getArticleInformation($article_id){
		$sql_get_article_information="select * from article_normal where article_id=$article_id;";
		$result_get_article_information=mysql_query($sql_get_article_information) or die(mysql_error());
		$row_get_article_information=mysql_fetch_row($result_get_article_information);
		if(!empty($row_get_article_information)){
			return join("|",$row_get_article_information);
		}
		return "";
	}
}
